package cn.cdnsu.org.controller;

import cn.cdnsu.auth.annotation.NsuPermission;
import cn.cdnsu.base.util.AjaxResult;
import cn.cdnsu.base.util.PageInfo;
import cn.cdnsu.org.domain.Employee;
import cn.cdnsu.org.dto.EmployeeDTO;
import cn.cdnsu.org.query.EmployeeQuery;
import cn.cdnsu.org.service.IEmployeeServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
@NsuPermission(name="员工管理api")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeServiceI service;

    @PutMapping
    public AjaxResult QueryPage(@RequestBody EmployeeQuery employeeQuery) {
        try {
            PageInfo<Employee> pageInfo = service.queryPage(employeeQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @NsuPermission(name = "查询指定员工")
    @GetMapping("/{id}")
    public AjaxResult selectOneById(@PathVariable Long id) {
        try {
            Employee employee = service.selectOneById(id);
            return AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @NsuPermission(name = "查询所有员工")
    @GetMapping
    public AjaxResult selectAll() {
        try {
            List<Employee> employees = service.selectAll();
            return AjaxResult.success(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @NsuPermission(name = "删除员工")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @NsuPermission(name = "新增或修改员工")
    @PostMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (Objects.nonNull(employee)) {
                if (Objects.nonNull(employee.getId())) {
                    service.update(employee);
                } else {
                    service.insert(employee);
                }
            } else {
                return AjaxResult.error("请传入正确的参数");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @NsuPermission(name = "员工批量删除")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/role/{id}")
    public AjaxResult selectByEmployeeId(@PathVariable("id") Long id) {
        try {
            List<Long> roleids = service.selectByEmployeeId(id);
            return AjaxResult.success(roleids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/role")
    public AjaxResult insertByEmployeeId(@RequestBody EmployeeDTO employeeDTO) {
        try {
            service.insertByEmployeeId(employeeDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
